import { infos } from "@/apis/home"
import { onMounted, ref, reactive, toRefs } from "vue"
import { Toast } from "vant"
interface swoprType {
    carouselUrl: string,
    redirectUrl: string
}
interface newGoodsesType {
    goodsCoverImg: string,
    goodsId: number,
    goodsIntro: string,
    goodsName: string,
    sellingPrice: number,
    tag: string
}

interface baseType {
    swiperList: Array<swoprType>,
    newGoodses: Array<newGoodsesType>,
    hots: Array<newGoodsesType>,
    recommends: newGoodsesType[],
    loading: boolean,
    finished: boolean,
    refreshing: boolean
}
export default function baseHook() {
    let state = reactive<baseType>({
        swiperList: [],
        newGoodses: [],
        hots: [],
        recommends: [],
        loading: true,
        finished: false,
        refreshing: false
    })
    async function getInfos() {
        Toast.loading({
            duration: 0,
            message: '加载中...',
            forbidClick: true,
        });
        let { data } = await infos()
        state.refreshing = false
        Toast.clear();
        state.swiperList = data.carousels
        state.newGoodses = data.newGoodses
        state.hots = data.hotGoodses
        state.recommends = data.recommendGoodses
        state.loading=false
    }
    const onLoad = function () {

    }
    const onRefresh = function () {
        getInfos()
    }

    onMounted(() => {
        setTimeout(() => {
            getInfos()
        }, 1000);
    })
    return { ...toRefs(state), onLoad, onRefresh }
}
